﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Entity.Items;

namespace Spinning.Spinning.Entity.Scenes.SinglePlayer
{
    class SinglePlayerStage1Scene : SinglePlayerScene
    {
        List<Vector2> bonePosList = new List<Vector2>();
        List<Vector2> duckweedPosList = new List<Vector2>();
        List<Vector2> floodWoodPosList = new List<Vector2>();
        List<Vector2> octopusPosList = new List<Vector2>();
        List<Vector2> shoalPosList = new List<Vector2>();
        List<Vector2> accPosList = new List<Vector2>();
        List<Vector2> scorePosList = new List<Vector2>();

        public override void OnLoad()
        {
            base.OnLoad();
            ContinuousSprite backGroundSprite = new ContinuousSprite(Camera, 1);
            backGroundSprite.textureName = "singleStage1/w1_background";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Player.LifeTime = Game1.stageManager.LifeTimeEarnByUser * 5;

            setPosList();

            for (int i = 0; i < bonePosList.Count; i++)
            {
                StaticBarrier bone = new StaticBarrier();
                bone.textureName = "singleStage1/bone";
                bone.OnLoad();
                bone.ItemLevel = 2;
                bone.SetAnimation(new Point(1, 1));
                bone.OnCreatBody(World, PhysicConnectManager);
                bone.BodyPosiotion = bonePosList[i];
                this.Attach(bone);
            }

            for (int i = 0; i < duckweedPosList.Count; i++)
            {
                StaticBarrier duckweed = new StaticBarrier();
                duckweed.textureName = "singleStage1/duckweed";
                duckweed.OnLoad();
                duckweed.ItemLevel = 1;
                duckweed.SetAnimation(new Point(1, 1));
                duckweed.OnCreatBody(World, PhysicConnectManager);
                duckweed.BodyPosiotion = duckweedPosList[i];
                this.Attach(duckweed);
            }

            for (int i = 0; i < floodWoodPosList.Count; i++)
            {
                StaticBarrier floodWood = new StaticBarrier();
                floodWood.textureName = "singleStage1/floodwood";
                floodWood.OnLoad();
                floodWood.ItemLevel = 1;
                floodWood.SetAnimation(new Point(1, 1));
                floodWood.OnCreatBody(World, PhysicConnectManager);
                floodWood.BodyPosiotion = floodWoodPosList[i];
                this.Attach(floodWood);
            }

            for (int i = 0; i < octopusPosList.Count; i++)
            {
                StaticBarrier octopus = new StaticBarrier();
                octopus.textureName = "singleStage1/octopus";
                octopus.OnLoad();
                octopus.ItemLevel = 2;
                octopus.SetAnimation(new Point(1, 1));
                octopus.OnCreatBody(World, PhysicConnectManager);
                octopus.BodyPosiotion = octopusPosList[i];
                this.Attach(octopus);
            }

            for (int i = 0; i < shoalPosList.Count; i++)
            {
                StaticBarrier shoal = new StaticBarrier();
                shoal.textureName = "singleStage1/shoal";
                shoal.OnLoad();
                shoal.ItemLevel = 1;
                shoal.SetAnimation(new Point(1, 1));
                shoal.OnCreatBody(World, PhysicConnectManager);
                shoal.BodyPosiotion = shoalPosList[i];
                this.Attach(shoal);
            }

            for (int i = 0; i < accPosList.Count; i++)
            {
                AddTimeItem accItem = new AddTimeItem();
                accItem.textureName = "singleStage1/accelerate";
                accItem.OnLoad();
                accItem.ItemLevel = 5;
                accItem.SetAnimation(new Point(1, 1));
                accItem.OnCreatBody(World, PhysicConnectManager);
                accItem.BodyPosiotion = accPosList[i];
                this.Attach(accItem);
            }

            for (int i = 0; i < scorePosList.Count; i++)
            {
                AddScoreItem score = new AddScoreItem();
                score.textureName = "singleStage1/score";
                score.OnLoad();
                score.SetAnimation(new Point(1, 1));
                score.OnCreatBody(World, PhysicConnectManager);
                score.BodyPosiotion = scorePosList[i];
                score.OnGetScoreItemEvent += test;
                this.Attach(score);
            }

            Bound b1 = new Bound();
            b1.Size = new Vector2(Constants.Window_Width, 20);
            b1.OnCreatBound(World);
            b1.Position = new Vector2(Constants.Window_Width / 2, 1000);//下界
            Attach(b1);

            Bound b2 = new Bound();
            b2.Size = new Vector2(Constants.Window_Width, 20);
            b2.OnCreatBound(World);
            b2.Position = new Vector2(Constants.Window_Width / 2, -12000);//上界
            Attach(b2);

            SortChildren();
        }


        public void test() //測試 加分數事件
        {
            Player.AddLifeTime(0);
        }

        public override Scene Clone()
        {
            Scene scene = new SinglePlayerStage1Scene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }

        void setPosList()
        {
            int[] posX = new int[] { 60, 180, 300, 420 };

            Random random = new Random();

            Vector2 v;
            for (int i = 0; i < 30; i++)
            {
                v = new Vector2(posX[random.Next(0, 4)], -400 * i);
                switch (i%10) 
                {
                    case 0:
                        bonePosList.Add(v);
                        break;
                    case 1:
                        accPosList.Add(v);
                        break;
                    case 2:
                        scorePosList.Add(v);
                        break;
                    case 3:
                        bonePosList.Add(v);
                        break;
                    case 4:
                        scorePosList.Add(v);
                        break;
                    case 5:
                        floodWoodPosList.Add(v);
                        break;
                    case 6:
                        duckweedPosList.Add(v);
                        break;
                    case 7:
                        shoalPosList.Add(v);
                        break;
                    case 8:
                        scorePosList.Add(v);
                        break;
                    case 9:
                        octopusPosList.Add(v);
                        break;
                }
            }
        }
    }
}
